package com.purong.purong.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.BankListBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.bean.UserCreditBean;
import com.purong.purong.util.ZeroStartUnableTextWatch;
import com.purong.purong.widget.TextChoseDialog;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/15.
 */
public class UserCreditAddActivity extends BaseActivity {
    private EditText amountEd,usedAmountEd;
    private TextView bankTv,countTv;
    private Button confirm;
    private TextChoseDialog mBankDialog;
    private TextItemBean bankBean;
    private List<TextItemBean> itemBeanList=new ArrayList<>();
    private UserCreditBean creditBean;
//    private int count;
    @Override
    public void initBundle() {
        creditBean=(UserCreditBean)getIntent().getSerializableExtra("USER_CREDIT");
//        count=getIntent().getIntExtra("COUNT",-1);
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_user_credit_add;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("信用卡信息");
//        countTv=(TextView)findViewById(R.id.count);
        amountEd=(EditText)findViewById(R.id.amount_ed);
        usedAmountEd=(EditText)findViewById(R.id.used_amount_ed);
        amountEd.addTextChangedListener(new ZeroStartUnableTextWatch(amountEd));
        usedAmountEd.addTextChangedListener(new ZeroStartUnableTextWatch(usedAmountEd));
        bankTv=(TextView)findViewById(R.id.bank_tv);
        confirm=(Button)findViewById(R.id.confirm);
        if (creditBean==null){
            confirm.setText("保存，进入下一信用卡");
        }else {
            confirm.setText("保存");
        }
//        if (count!=-1){
//            countTv.setText("信用卡"+count);
//        }else {
//            countTv.setText("信用卡");
//        }
    }
    private void getBank(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        request(Contant.BANK_LIST,params,true,true);
    }
    private void addCredit(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (bankBean==null){
            showToast("信用卡发卡行为必选项");
            return;
        }else {
            params.put("bank_id",bankBean.getId());
        }
        if (amountEd.getText().toString().trim().equals("")){
            showToast("信用卡额度为必填项");
            return;
        }else if (Integer.valueOf(amountEd.getText().toString().trim())==0){
            showToast("信用卡额度不能为0");
            return;
        }else {
            params.put("amount",amountEd.getText().toString().trim());
        }
        if (!usedAmountEd.getText().toString().trim().equals("")&&!amountEd.getText().toString().trim().equals("")&&
                Integer.valueOf(usedAmountEd.getText().toString().trim())>Integer.valueOf(amountEd.getText().toString().trim())){
            showToast("已使用额度不能大于信用卡额度");
            return;
        }else {
            params.put("used_amount",usedAmountEd.getText().toString().trim());
        }
        request(Contant.CREDIT_CARD_ADD,params,true,true);
    }
    private void editCredit(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_credit_card_id",creditBean.getUser_credit_card_id());
        if (bankBean==null){
            showToast("信用卡发卡行为必选项");
            return;
        }else {
            params.put("bank_id",bankBean.getId());
        }
        if (amountEd.getText().toString().trim().equals("")){
            showToast("信用卡额度为必填项");
            return;
        }else if (Integer.valueOf(amountEd.getText().toString().trim())==0){
            showToast("信用卡额度不能为0");
            return;
        }else {
            params.put("amount",amountEd.getText().toString().trim());
        }
        if (!usedAmountEd.getText().toString().trim().equals("")&&!amountEd.getText().toString().trim().equals("")&&
                Integer.valueOf(usedAmountEd.getText().toString().trim())>Integer.valueOf(amountEd.getText().toString().trim())){
            showToast("已使用额度不能大于信用卡额度");
            return;
        }else {
            params.put("used_amount",usedAmountEd.getText().toString().trim());
        }
        request(Contant.CREDIT_CARD_EDIT,params,true,true);
    }

    private void setData(){
        if (creditBean==null)
            return;
        bankBean=new TextItemBean();
        bankBean.setId(creditBean.getBank_id());
        bankBean.setName(creditBean.getBank_name());
        bankTv.setText(bankBean.getName());
        bankTv.setTextColor(getResources().getColor(R.color.blue));
        if (creditBean.getAmount()!=null){
            amountEd.setText(creditBean.getAmount());
        }
        if (creditBean.getUsed_amount()!=null){
            usedAmountEd.setText(creditBean.getUsed_amount());
        }
    }
    private void initBankDialog(){
        mBankDialog=new TextChoseDialog(this);
        mBankDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                bankBean=item;
                bankTv.setText(bankBean.getName());
                bankTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });

        mBankDialog.setData(itemBeanList);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.BANK_LIST)){
            if (responseInfo.isRequestOk){
                try {
                    BankListBean bean= JSON.parseObject(responseInfo.content, BankListBean.class);
                    if (bean!=null&&bean.getBank_list()!=null){
                        TextItemBean bankBean;
                        for (int i = 0; i <bean.getBank_list().size() ; i++) {
                            bankBean=new TextItemBean();
                            bankBean.setName(bean.getBank_list().get(i).getBank_name());
                            bankBean.setId(bean.getBank_list().get(i).getBank_id());
                            itemBeanList.add(bankBean);
                        }
                        initBankDialog();
                        mBankDialog.show();
                    }
                }catch (Exception e){

                }
            }
        }else if (responseInfo.url.equals(Contant.CREDIT_CARD_ADD)){
            if (responseInfo.isRequestOk){
                showToast("添加成功");
//                count++;
                Intent intent=new Intent(this,UserCreditAddActivity.class);
//                intent.putExtra("COUNT",count);
                startActivity(intent);
                this.finish();
            }
        }else if (responseInfo.url.equals(Contant.CREDIT_CARD_EDIT)){
            if (responseInfo.isRequestOk){
                showToast("编辑成功");
                this.finish();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (creditBean!=null){
                    editCredit();
                }else {
                    addCredit();
                }
                break;
            case R.id.bank_lay:
                if (mBankDialog==null){
                    getBank();
                }else {
                    mBankDialog.show();
                }
                break;
        }
    }
    @Override
    public void initData() {
        if(creditBean!=null){
            setData();
        }
    }
}
